package jdbc;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2018-07-30 21:14
 */
public class DaoTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(DaoTest.class);
    private Dao<Customer> dao = new DaoImpl<>();

    @Test
    public void testInsert() {
        Customer customer = new Customer();
        customer.setName("test");
        customer.setEmail("test@124.com");
        customer.setBirth("1984-09-08");
        String sql = "INSERT INTO customers (name, email,birth) VALUES(?,?,?)";
        dao.update(sql, customer.getName(), customer.getEmail(), customer.getBirth());
    }

    @Test
    public void testGet() {
        String sql = "SELECT id, name, email, birth FROM customers WHERE id=?";
        Customer customer = dao.get(Customer.class, sql, 2);
        LOGGER.debug(customer.toString());
    }

    @Test
    public void testGetList() {
        String sql = "SELECT id, name, email, birth FROM customers";
        List<Customer> customers = dao.getForList(Customer.class, sql);
        customers.forEach(System.out::println);
    }

    @Test
    public void testGetValue() {
        String sql = "SELECT COUNT(id) FROM customers";
        Long count = dao.getForValue(sql);
        LOGGER.debug("Query total count is " + count);

        sql = "SELECT email FROM customers WHERE id=?";
        String email = dao.getForValue(sql, 5);
        LOGGER.debug("Query email is " + email);
    }

}
